package org.jsbd.boss.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.domian.DaylimitBeanVO;
import org.jsbd.boss.domian.NewExecutionGroup;
import org.jsbd.boss.domian.NewExecutionGroupMark;
import org.jsbd.boss.domian.channel.ReaderStatisticsDto;
import org.jsbd.boss.domian.channel.ReaderStatisticsVO;
import org.jsbd.boss.service.INewExecutionGroupService;
import org.jsbd.boss.service.IReaderStatisticsService;
import org.jsbd.boss.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/readerStatistics")
public class ReaderStatisticsController {

	private static final Logger logger = LoggerFactory.getLogger(ReaderStatisticsController.class);

	private static Map<String, Float> standardMap = new HashMap<String, Float>();

	static {
		standardMap.put("00", 4.39f);
		standardMap.put("01", 2.71f);
		standardMap.put("02", 1.77f);
		standardMap.put("03", 1.19f);
		standardMap.put("04", 0.96f);
		standardMap.put("05", 1.05f);
		standardMap.put("06", 1.75f);
		standardMap.put("07", 2.84f);
		standardMap.put("08", 3.64f);
		standardMap.put("09", 4.50f);
		standardMap.put("10", 5.07f);
		standardMap.put("11", 5.06f);
		standardMap.put("12", 5.55f);
		standardMap.put("13", 5.83f);
		standardMap.put("14", 5.58f);
		standardMap.put("15", 5.46f);
		standardMap.put("16", 5.29f);
		standardMap.put("17", 4.96f);
		standardMap.put("18", 4.48f);
		standardMap.put("19", 4.41f);
		standardMap.put("20", 5.17f);
		standardMap.put("21", 6.08f);
		standardMap.put("22", 6.52f);
		standardMap.put("23", 5.76f);
	}

	@Resource
	private IReaderStatisticsService readerStatisticsService;
	@Resource
	private INewExecutionGroupService newExecutionGroupService;

	/**
	 * 图表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list() {
		return "readerStatistics/list";
	}
	
	/**
	 * 图表数据
	 * 
	 * @param model
	 *            model
	 * @param flowVo
	 *            查询条件
	 * @return
	 */
	@RequestMapping(value = "/listData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> listData(Model model, ReaderStatisticsVO readerStatisticsVO) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		String month = readerStatisticsVO.getMonth();
		String groupName = readerStatisticsVO.getGroupName();
		String mark = readerStatisticsVO.getMark();
		
		String date = DateUtil.getY_M_DStr();
		if (StringUtils.isBlank(month)) {
			month = date.substring(0, 7);
		}
		
		List<String> marks = new ArrayList<String>();
		if (!StringUtils.isBlank(groupName)) {
			NewExecutionGroup newExecutionGroup= newExecutionGroupService.getGroupByName(groupName);
			if (null != newExecutionGroup) {
				List<NewExecutionGroupMark> groupMarks = newExecutionGroupService.getGroupMarkById(newExecutionGroup.getId());
				if (groupMarks.size() == 0 || null == groupMarks) {
					map.put("divTotal", 0);
					map.put("xAxis", 0);
					map.put("yAxis", 0);
					return map;
				}
				
				for (NewExecutionGroupMark megm : groupMarks) {
					marks.add(megm.getMark());
				}
			} else {
				map.put("divTotal", 0);
				map.put("xAxis", 0);
				map.put("yAxis", 0);
				return map;
			}
		}
		if (!StringUtils.isBlank(mark)) {
			marks.add(readerStatisticsVO.getMark());
		}
		
		readerStatisticsVO.setMarks(marks);
		readerStatisticsVO.setMonth(month);
		Integer monthNum = readerStatisticsService.getMonthNum(readerStatisticsVO);
		readerStatisticsVO.setMonthNum(monthNum);
		
		List<String> xAxis = new ArrayList<String>();
		List<ReaderStatisticsDto> yAxis = readerStatisticsService.selectYAxis(readerStatisticsVO);
		float divTotal = 0f;
		for (ReaderStatisticsDto rsDto : yAxis) {
			String hour = rsDto.getHour();
			hour = hour.substring(11, 13);
			Float standard = standardMap.get(hour);
			xAxis.add(hour);
			rsDto.setStandard(standard);
			float div = Math.abs(standard - rsDto.getRate());
			divTotal += div;
			rsDto.setDiv(div);
		}
		
		map.put("divTotal", divTotal);
		map.put("xAxis", xAxis);
		map.put("yAxis", yAxis);
		return map;
	}

	/**
	 * 阅读付费数据
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/listJson", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> listJson(ReaderStatisticsVO readerStatisticsVO) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		String month = readerStatisticsVO.getMonth();
		String groupName = readerStatisticsVO.getGroupName();
		String mark = readerStatisticsVO.getMark();
		
		String date = DateUtil.getY_M_DStr();
		if (StringUtils.isBlank(month)) {
			month = date.substring(0, 7);
		}
		
		List<String> marks = new ArrayList<String>();
		if (!StringUtils.isBlank(groupName)) {
			NewExecutionGroup newExecutionGroup= newExecutionGroupService.getGroupByName(groupName);
			if (null != newExecutionGroup) {
				List<NewExecutionGroupMark> groupMarks = newExecutionGroupService.getGroupMarkById(newExecutionGroup.getId());
				if (groupMarks.size() == 0 || null == groupMarks) {
					result.put("rows", new ArrayList<ReaderStatisticsDto>());
					result.put("total", 0);
					return result;
				}
				
				for (NewExecutionGroupMark megm : groupMarks) {
					marks.add(megm.getMark());
				}
			} else {
				result.put("rows", new ArrayList<ReaderStatisticsDto>());
				result.put("total", 0);
				return result;
			}
		}
		if (!StringUtils.isBlank(mark)) {
			marks.add(readerStatisticsVO.getMark());
		}
		
		readerStatisticsVO.setMarks(marks);
		readerStatisticsVO.setMonth(month);
		Integer monthNum = readerStatisticsService.getMonthNum(readerStatisticsVO);
		readerStatisticsVO.setMonthNum(monthNum);
		
		List<ReaderStatisticsDto> yAxis = readerStatisticsService.selectYAxis(readerStatisticsVO);
		
		result.put("rows", yAxis);
		result.put("total", 24);
		return result;
	}

}
